package com.xxkj.drugtraceability.ui.departhd

import android.app.Application
import androidx.fragment.app.Fragment
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.xxhis.drugtraceability.base.BaseViewModel
import com.xxkj.drugtraceability.bean.*
import com.xxkj.drugtraceability.net.Result
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * author: zhoujingjin
 * created on: 2024/9/10 16:15
 * description:
 */
class DepartDrugHdCollectionViewModel(application: Application, private val repository: DepartDrugHdCollectionRepository):BaseViewModel(application) {
    var unDataList = MutableLiveData<MutableList<DepartDrugInfo>?>()
    var dataList = MutableLiveData<MutableList<DepartDrugInfo>?>()
    var baseUiModel = MutableLiveData<BaseUiModel<Boolean>>()
    //是否可以扫溯源码，如果处方码已经保存过追溯码，无论保存的多少，都不再让保存了。
    var Prescriptionbarcode = ""
    var viewpagerIndex = MutableLiveData(0)
    var mFragmentList = mutableListOf<Fragment>()
    var departData = MutableLiveData<DepartDrug?>()
    var isHd = false
    fun getPrescriptionInfo(barcode:String){
        viewModelScope.launch(Dispatchers.Main) {
            withContext(Dispatchers.IO) {
                baseUiModel.postValue(BaseUiModel(showLoading = true))
                val result = repository.queryPrescriptionInfoList(Gson().toJson(DepartDrugRequest("",barcode)))
                if (result is Result.Success){
                    if (result.data.data.bqdm.isNullOrEmpty()){
                        departData.postValue(null)
                        baseUiModel.postValue(BaseUiModel(showLoading = false,showError = "请检查领药单是否正确"))
                    }else{
                        var collection= false
                        result.data.data.noEnterDrugInfoList?.forEach {
                            if (it.isEnter=="1"){
                                collection = true
                            }
                        }
                        if (!result.data.data.enterDrugInfoList.isNullOrEmpty()){
                            collection = true
                        }
                        if (!collection){
                            //未采集，提醒
                            departData.postValue(null)
                            baseUiModel.postValue(BaseUiModel(showLoading = false,showError = "该领药单未采集，无法核对"))
                            return@withContext
                        }
                        departData.postValue(result.data.data)
                        Prescriptionbarcode = barcode
                        dataList.postValue(result.data.data.enterDrugInfoList as MutableList<DepartDrugInfo>?)
                        unDataList.postValue(result.data.data.noEnterDrugInfoList as MutableList<DepartDrugInfo>?)
                        baseUiModel.postValue(BaseUiModel(showLoading = false))
                    }

                }else if (result is Result.Error){
                    baseUiModel.postValue(BaseUiModel(showLoading = false,showError = result.errorData?:result.exception.message))
                }
            }
        }
    }
    /**
     * 保存数据
     */
    fun finishScanDrugCode(finishRequest: DepartHdRequest){
        viewModelScope.launch(Dispatchers.Main) {
            withContext(Dispatchers.IO) {
                baseUiModel.postValue(BaseUiModel(showLoading = true))
                val result = repository.finishScanDrugCode(GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().toJson(finishRequest))
                if (result is Result.Success){
                    if (result.data.data.isSuc=="1"){
                        baseUiModel.postValue(BaseUiModel(showLoading = false,showSuccess = true))
                        Prescriptionbarcode = ""
                        viewpagerIndex.postValue(0)
                        departData.postValue(null)
                        Prescriptionbarcode = ""
                        isHd = false
                        dataList.postValue(mutableListOf())
                        unDataList.postValue(mutableListOf())
                    }else{
                        var message = ""
                        result.data.data.message.forEach {
                            message+=it+"\n"
                        }
                        baseUiModel.postValue(BaseUiModel(showLoading = false,showError = message))
                    }

                }else if (result is Result.Error){
                    baseUiModel.postValue(BaseUiModel(showLoading = false,showError = result.errorData?:result.exception.message))
                }
            }
        }
    }
}